/*
 * SqEngine 0.5.1
 * Date 27/08/2009 
 * by squid <squid@stormwind.it>
 * web: http://squareengine.codeplex.com
 * Basic Entity Component
 */

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Utils;
using SqEngine.Components;
using sqengine.Components;
using sqengine.Engine;
using sqengine.Engine.Common;
#endregion



namespace sqengine.PhysicObjects
{
    #region Action Event Class
    public class ActionArg : System.EventArgs
    {
        private PlayerStats player;

        public ActionArg(PlayerStats p)
        {
            this.player = p;
        }

        public PlayerStats Player()
        {
            return player;
        }
    }

    public class MouseArg : System.EventArgs
    {
        private Vector3 _ray;
        private Vector3 _position;
        private Vector3 _normal;
        private float _frac;
        private float _distance;

        public MouseArg(float distance, Vector3 ray, Vector3 position, Vector3 normal, float frac)
        {
            this._ray = ray;
            this._position = position;
            this._normal = normal;
            this._frac = frac;
            this._distance = distance;
        }

        public Vector3 Ray()
        {
            return _ray;
        }

        public Vector3 Position()
        {
            return _position;
        }

        public Vector3 Normal()
        {
            return _normal;
        }

        public float Frac()
        {
            return _frac;
        }

        public float Distance()
        {
            return _distance;
        }

    }



    #endregion

    public abstract class Entity : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Game _game;
        private AdvancedContent _adcontent;
        private WebBrowserPool _webbrowserpool;
        

        private Vector3 position;
        private Vector3 rotation;
        private Vector3 scale;
        private Matrix localWorld;
        private Vector3 color;
        private string name;

        //If -1 then can be activated from every distance
        //Default distance is 4.0f
        private float action_distance = 4.0f;

        public Matrix LocalWorld { get { return localWorld; } set { localWorld = value; } }

        public Vector3 Scale { get { return scale; } set { scale = value; } }
        public string Name { get { return name; } set { name = value; } }
        public Vector3 Rotation { get { return rotation; } set { rotation = value; } }
        public Vector3 Position { get { return position; } set { position = value; } }
        public Vector3 ObjColor { get { return color; } set { color = value; } }
        public float ActionDistance { get { return action_distance; } set { action_distance = value; } }

        public void RotationX(float rotX) { rotation.X = rotX; }
        public void RotationY(float rotY) { rotation.Y = rotY; }
        public void RotationZ(float rotZ) { rotation.Z = rotZ; }

        public Game CurrentGame { get { return _game; } set { _game = value; } }
        public AdvancedContent Adcontent { get { return _adcontent; } set { _adcontent = value; } }
        public WebBrowserPool CurrentWebBrowserPool { get { return _webbrowserpool; } }

        protected static Random random = new Random();

        #region Events
        // delegate declaration
        public delegate void ActionHandler(object sender, ActionArg ca);
        public delegate void MouseEventHandler(object sender, MouseArg e);

        public event ActionHandler OnAction;
        public event MouseEventHandler OnMouseOver;
        public event MouseEventHandler OnMouseLeave;

        #endregion

        public Entity(Game game)
            : base(game)
        {
            this._game = game;
            this._adcontent = ((sqengine)game).AdContent;
            this._webbrowserpool = ((sqengine)game).WebBrowserQueue;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {

            base.Update(gameTime);
        }

        public void Action(PlayerStats player)
        {
            if (OnAction != null)            
                OnAction(this, new ActionArg(player));
            
        }

        public void MouseOver(float distance, Vector3 ray, Vector3 position, Vector3 normal, float frac)
        {
            if (OnMouseOver != null)
                OnMouseOver(this, new MouseArg(distance, ray, position, normal, frac));
        }

        public void MouseLeave(float distance, Vector3 ray, Vector3 position, Vector3 normal, float frac)
        {
            if (OnMouseLeave != null)
                OnMouseLeave(this, new MouseArg(distance, ray, position, normal, frac));
        }

    }
}